//
//  TKIMTextField.m
//  TKIM
//
//  Created by liupm on 15/10/28.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMTextField.h"

@implementation TKIMTextField

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        self.delegate = self;
        _maxLimitLength = 100;
        _underlineColor = [UIColor lightGrayColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    if(_showUnderline)
    {
        CGFloat scale = [[UIScreen mainScreen] scale];
        CGFloat width = scale > 0.0 ? 1.0 / scale : 1.0;
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context,_underlineColor.CGColor);
        CGContextFillRect(context, CGRectMake(0, CGRectGetHeight(self.frame) - width, CGRectGetWidth(self.frame), 3));
    }
}

#pragma mark  输入代理 

/**
 *  @Author lpm, 15-10-28 11:10:52
 *
 *  @brief  输入限制
 *
 *  @param textField
 *  @param range
 *  @param string
 *
 *  @return
 */
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
//    NSUInteger length=[textField.text lengthOfBytesUsingEncoding:enc];
//    NSUInteger addLength=[string lengthOfBytesUsingEncoding:enc];
//    
//    if((length>= _maxLimitLength && addLength>0) || length+addLength>_maxLimitLength)
//    {
//        return NO;
//    }
//    
//    return YES;
//}



/**
 *  @Author lpm, 15-10-28 11:10:06
 *
 *  @brief  点击完成
 *
 *  @param textField
 *
 *  @return
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignFirstResponder];
    return YES;
}


/**
 *  @Author lpm, 16-04-26 15:04:04
 *
 *  @brief  重写返回键方法
 */
-(void)deleteBackward
{
    if(_imTextFieldDelegate && [_imTextFieldDelegate respondsToSelector:@selector(textFieldDeleteBackward:)])
    {
        [_imTextFieldDelegate textFieldDeleteBackward:self];
    }
    [super deleteBackward];
    if(_imTextFieldDelegate && [_imTextFieldDelegate respondsToSelector:@selector(textFieldChange:)])
    {
        [_imTextFieldDelegate textFieldChange:self.text];
    }
}



@end
